/*************************************************************************
> File Name: mystring.cpp
> Author: hzy
> Created Time: 2023-08-12 11:21:29
> Description:
************************************************************************/

#include <iostream>
#include <cstring>
using namespace std;

class mystring
{
private:
    int length;
    char *data;

public:
    // 无参构造
    mystring()
    {
        length = 0;
        data = new char[length + 1];
        *data = '\0';
    }

    ~mystring()
    {
        if (data != NULL)
        {
            delete[] data;
        }
    }

    // 有参构造
    mystring(const char *str)
    {
        if (str == NULL)
        {
            length = 0;
            data = new char[length + 1];
            *data = '\0';
        }
        else
        {
            length = strlen(str);
            data = new char[length + 1];
            strcpy(data, str);
        }
    }

    // 拷贝构造
    mystring(const mystring &str)
    {
        length = str.length;
        data = new char[length + 1];
        strcpy(data, str.data);
    }

    // 成员函数运算符重载
public:
    char &operator[](int i);
    mystring &operator=(const mystring &str);
    mystring &operator+=(const mystring &str);

    // 友元声明
    friend ostream &operator<<(ostream &os, const mystring &str);
    friend istream &operator>>(istream &is, const mystring &str);
    friend mystring operator+(const mystring &str1, const mystring &str2);
    friend bool operator>(const mystring &str1, const mystring &str2);
};

mystring &mystring::operator=(const mystring &str)
{
    this->length = str.length;
    if (this->data != NULL)
    {
        delete[] this->data;
    }
    this->data = new char[length + 1];
    strcpy(data, str.data);

    return *this;
}

mystring &mystring::operator+=(const mystring &str)
{
    mystring temp(*this);

    this->length += str.length;
    if (this->data != NULL)
    {
        delete[] this->data;
    }
    this->data = new char[length + 1];
    strcpy(this->data, temp.data);
    strcat(this->data, str.data);
}

char &mystring::operator[](int i)
{
    return data[i];
}

ostream &operator<<(ostream &os, const mystring &str)
{
    os << str.data;
    return os;
}
istream &operator>>(istream &is, const mystring &str)
{
    is >> str.data;
    return is;
}
// 重载+运算符
mystring operator+(const mystring &str1, const mystring &str2)
{
    mystring result;
    result.length = str1.length + str2.length;
    result.data = new char[result.length + 1];
    strcpy(result.data, str1.data);
    strcat(result.data, str2.data);

    return result;
}

bool operator>(const mystring &str1, const mystring &str2)
{
    return (strcmp(str1.data, str2.data) > 0);
}

int main()
{
    mystring str;
    mystring str1("abcd");
    mystring str2("efgh");

    str1[2] = 'P';

    cout << str1 << endl;

    str = str1 + str2;
    cout << str << endl;

    if (str1 > str2)
    {
        cout << "str1 > str2" << endl;
    }
    else
    {
        cout << "str1 < str2" << endl;
    }

    str += str2;
    cout << str1 << endl;

    cin >> str;
    cout << str << endl;

    return 0;
}
